﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ThreeByte.ActiveDeck.Service;
using System.ComponentModel;

namespace ThreeByte.ActiveDeck.Config
{
    public class EnvironmentContainer : INotifyPropertyChanged
    {
        private static readonly XNamespace ns = ServiceConfiguration.NAMESPACE;

        public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        public event EventHandler PropertiesPaneDisabled;
        public event EventHandler PropertiesPaneEnabled;


        public bool ShowTaskPane { get; set; }

        private bool showPropertiesPane;
        public bool ShowPropertiesPane {
            get {
                return showPropertiesPane;
            }
            set {
                bool oldValue = showPropertiesPane;
                showPropertiesPane = value;
                if(showPropertiesPane != oldValue) {
                    NotifyPropertyChanged("ShowPropertiesPane");
                    //Also raise a specific event
                    if(showPropertiesPane) {
                        if(PropertiesPaneEnabled != null){
                            PropertiesPaneEnabled(this, EventArgs.Empty);
                        }
                    } else {
                        if(PropertiesPaneDisabled != null){
                            PropertiesPaneDisabled(this, EventArgs.Empty);
                        }
                    }
                }
            }
        }

        public bool ShowInteractivePresentationMonitor { get; set; }

        public EnvironmentContainer() {
            //ShowTaskPane = true;
            ShowInteractivePresentationMonitor = true; //By default
        }

        public XElement ToXML() {
            XElement node = new XElement(ns + "Environment");
            node.Add(new XAttribute("ShowTaskPane", ShowTaskPane));
            node.Add(new XAttribute("ShowPropertiesPane", ShowPropertiesPane));
            node.Add(new XAttribute("ShowInteractivePresentationMonitor", ShowInteractivePresentationMonitor));
            return node;
        }

        public static EnvironmentContainer FromXML(XElement node) {
            EnvironmentContainer env = new EnvironmentContainer();
            env.ShowTaskPane = bool.Parse(node.Attribute("ShowTaskPane").Value);
            //TODO: Remove these conditionals
            if(node.Attribute("ShowPropertiesPane") != null) {
                env.ShowPropertiesPane = bool.Parse(node.Attribute("ShowPropertiesPane").Value);
            }
            if(node.Attribute("ShowInteractivePresentationMonitor") != null) {
                env.ShowInteractivePresentationMonitor = bool.Parse(node.Attribute("ShowInteractivePresentationMonitor").Value);
            }
            return env;
        }

    }
}
